package ecom.appcode.web.action;

import java.io.IOException;
import java.util.List;

import javax.servlet.RequestDispatcher;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;

import ecom.appcode.business.ShoesLogic;
import ecom.appcode.business.ShoesLogicImpl;
import ecom.appcode.business.SliderLogic;
import ecom.appcode.business.SliderLogicImpl;
import ecom.appcode.common.daobean.ShoesDaoBean;
import ecom.appcode.common.daobean.SliderDaoBean;

/**
 * Servlet implementation class SliderManagerAction
 */
public class SliderManagerAction extends HttpServlet {
    private static final long serialVersionUID = 1L;

    /**
     * @see HttpServlet#HttpServlet()
     */
    public SliderManagerAction() {
        super();
        // TODO Auto-generated constructor stub
    }

    /**
     * @see HttpServlet#doGet(HttpServletRequest request, HttpServletResponse response)
     */
    protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        // TODO Auto-generated method stub
        String action = request.getParameter("btAction");
        if (action.equals("ViewAllSlider")) {
            SliderLogic sliderLogic = new SliderLogicImpl();
            List<SliderDaoBean> Sliderlist = sliderLogic.loadAllSlider();
            if (Sliderlist != null) {
                HttpSession session = request.getSession();
                session.setAttribute("LOAD_SLIDER", Sliderlist);
                RequestDispatcher rd = request.getRequestDispatcher("slider-management.jsp");
                rd.forward(request, response);
            }
        } else if(action.equals("DeleteSlider")) {
            SliderLogic sliderLogic = new SliderLogicImpl();
            int sliderID = Integer.parseInt(request.getParameter("txtSliderID"));
            boolean result = sliderLogic.deleteSlider(sliderID);
            List<SliderDaoBean> Sliderlist = sliderLogic.loadAllSlider();
            if(result) {
                HttpSession session = request.getSession();
                session.setAttribute("LOAD_SLIDER", Sliderlist);
                RequestDispatcher rd = request.getRequestDispatcher("slider-management.jsp");
                rd.forward(request, response);
            }

        }
    }

    /**
     * @see HttpServlet#doPost(HttpServletRequest request, HttpServletResponse response)
     */
    protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException,
            IOException {
        // TODO Auto-generated method stub
        String action = request.getParameter("btAction");
        if ("Submit".equals(action)) {
            String image = "images/featured/" + request.getParameter("txtImage");
            String description = request.getParameter("txtDescription");
            SliderLogic sliderLogic = new SliderLogicImpl();
            boolean result = sliderLogic.addNewSlider(image, description);
            if (result) {
                List<SliderDaoBean> Sliderlist = sliderLogic.loadAllSlider();
                HttpSession session = request.getSession();
                session.setAttribute("LOAD_SLIDER", Sliderlist);
                RequestDispatcher rd = request.getRequestDispatcher("slider-management.jsp");
                rd.forward(request, response);
            }
        }
    }

}
